home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 February: Tool Chest / Dev.CD Feb 00 TC.toast / pc / what's new? / sample code / games / netsprockettest / events.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-20  |  9.2 KB  |  469 lines

  1. /*************************************************************************************
  2. #
  3. #        events.c
  4. #
  5. #        This segment handles the basic event calls.
  6. #
  7. #        Author(s):     Michael Marinkovich
  8. #                    Apple Developer Technical Support
  9. #                    marink@apple.com
  10. #
  11. #        Modification History: 
  12. #
  13. #            2/10/96        MWM     Initial coding                     
  14. #
  15. #        Copyright © 1992-96 Apple Computer, Inc., All Rights Reserved
  16. #
  17. #
  18. #        You may incorporate this sample code into your applications without
  19. #        restriction, though the sample code has been provided "AS IS" and the
  20. #        responsibility for its operation is 100% yours.  However, what you are
  21. #        not permitted to do is to redistribute the source as "DSC Sample Code"
  22. #        after having made changes. If you're going to re-distribute the source,
  23. #        we require that you make it clear in the source that the code was
  24. #        descended from Apple Sample Code, but that you've made changes.
  25. #
  26. *************************************************************************************/
  27.  
  28. #include <Devices.h>
  29. #include <DiskInit.h>
  30. #include <Events.h>
  31. #include <Gestalt.h>
  32. #include <MacWindows.h>
  33. #include "NetStuff.h"
  34. #include <OSUtils.h>
  35. #include <Palettes.h>
  36. #include <ToolUtils.h>
  37.  
  38. #include "App.h"
  39. #include "Proto.h"
  40.  
  41. #include <sioux.h>
  42.  
  43. //----------------------------------------------------------------------
  44. //
  45. //    Global
  46. //
  47. //----------------------------------------------------------------------
  48.  
  49. extern Boolean        gInBackground;
  50. extern Boolean        gDone;
  51. extern Boolean        gHasAbout;        // have an about box?
  52.  
  53.  
  54. //----------------------------------------------------------------------
  55. //
  56. //    EventLoop - main entry and loop for all event processing
  57. //
  58. //
  59. //----------------------------------------------------------------------
  60.  
  61. void EventLoop(void)
  62. {
  63.     EventRecord        event;
  64.         
  65.     gDone = false;
  66.         
  67.     while (! gDone)
  68.     {
  69.         WaitNextEvent(everyEvent, &event, 60, nil);
  70.                                     
  71.         // go ahead and handle the event just 
  72.         // in case we have and idle
  73.         DoEvent(&event);
  74.  
  75.     }
  76. }
  77.  
  78.  
  79. //----------------------------------------------------------------------
  80. //
  81. //    MyGetSleep - return sleep value based upon whether or not the app
  82. //                 is in the background.
  83. //
  84. //----------------------------------------------------------------------
  85.  
  86. short MyGetSleep(void)
  87. {
  88.     short        sleep = 30;
  89.     
  90.     if (gInBackground)
  91.         sleep = 1L;
  92.  
  93.     return sleep;
  94.  
  95. }
  96.  
  97.  
  98. //----------------------------------------------------------------------
  99. //
  100. //    CustomWindowEvent - Handles custom procs assigned to a window. 
  101. //                        Different window kinds can easily have unique event
  102. //                        handlers, ie. floaters, dialogs, documentprocs
  103. //----------------------------------------------------------------------
  104.  
  105. void CustomWindowEvent(short eventType,WindowRef window,void *refCon)
  106. {
  107. #pragma unused (eventType, window, refCon)
  108. }
  109.  
  110.  
  111. //----------------------------------------------------------------------
  112. //
  113. //    DoEvent - event dispatcher, called by eventloop
  114. //                
  115. //
  116. //----------------------------------------------------------------------
  117.  
  118. void DoEvent(EventRecord *event)
  119. {
  120.     OSErr            err;
  121.     short            kind;
  122.     long            menuChoice;
  123.     Point            thePoint;
  124.     Boolean            active;
  125.     WindowRef        window;
  126.     
  127.     window = FrontWindow();
  128.  
  129.     SIOUXHandleOneEvent(event);
  130.  
  131.     switch(event->what) 
  132.     {
  133.         case nullEvent:
  134.             HandleNetwork();
  135.             break;
  136.             
  137.         case mouseDown:
  138.             HandleMouseDown(event);
  139.             break;
  140.                             
  141.         case mouseUp:
  142.             break;
  143.                             
  144.         case keyDown:
  145.         case autoKey:
  146.             if (event->modifiers & cmdKey) //    is cmd key down
  147.             {
  148.                 AdjustMainMenus();
  149.                 menuChoice = MenuKey(event->message & charCodeMask);
  150.                 kind = GetWindKind(window);
  151.                 if (kind < kDocKind || kind > kFloatKind)             // not our window
  152.                     HandleMenuChoice(window, (void *)&menuChoice);    // default menu
  153.                 else    
  154.                     CustomWindowEvent(kMenuProc, window, (void *)&menuChoice);
  155.             }
  156.             break;
  157.                             
  158.         case activateEvt:
  159.             gInBackground = event->modifiers & activeFlag;
  160.             active = gInBackground;
  161.             CustomWindowEvent(kActivateProc, (WindowRef)event->message, &active);
  162.             break;
  163.                             
  164.         case updateEvt:
  165.             UpdateWindow((WindowRef)event->message);
  166.             break;
  167.                             
  168.         case diskEvt:
  169.             if (HiWord(event->message) != noErr) 
  170.             {
  171.                 SetPt(&thePoint, 50, 50);
  172.                 err = DIBadMount(thePoint, event->message);
  173.             }
  174.             break;
  175.                             
  176.         case osEvt:
  177.             switch ((event->message >> 24) & 0x0FF) 
  178.             {        
  179.                 case suspendResumeMessage:    
  180.                     gInBackground = event->message & resumeFlag;
  181.                     active = gInBackground;
  182.                     CustomWindowEvent(kActivateProc, FrontWindow(), &active);
  183.                     break;
  184.             }
  185.             break;
  186.     
  187.         case kHighLevelEvent:
  188.             AEProcessAppleEvent(event);
  189.             break;
  190.     }
  191.  
  192. }            
  193.  
  194.  
  195.  
  196. //----------------------------------------------------------------------
  197. //
  198. //    DoIdle - handle Idle events
  199. //                
  200. //
  201. //----------------------------------------------------------------------
  202.  
  203. void DoIdle(WindowRef window, void *refCon)
  204. {
  205.  
  206. }
  207.  
  208.  
  209. //----------------------------------------------------------------------
  210. //
  211. //    HandleMouseDown - 
  212. //                
  213. //
  214. //----------------------------------------------------------------------
  215.  
  216. void HandleMouseDown(EventRecord *event)
  217. {
  218.     long            menuChoice;
  219.     short            thePart;
  220.     short            kind;
  221.     WindowRef        window;
  222.         
  223.  
  224.     thePart = FindWindow(event->where,&window);
  225.         
  226.     switch(thePart) 
  227.     {
  228.         case inMenuBar:
  229.             AdjustMainMenus();
  230.  
  231.             menuChoice = MenuSelect(event->where);
  232.             window = FrontWindow();
  233.             kind = GetWindKind(window);
  234.             if (kind < kDocKind || kind > kAboutKind)             // not our window
  235.                 HandleMenuChoice(window, (void *)&menuChoice);    // default menu
  236.             else    
  237.                 CustomWindowEvent(kMenuProc, window, (void *)&menuChoice);
  238.             break;
  239.  
  240.         case inContent:
  241.             if (window != FrontWindow())
  242.                 SelectWindow(window);
  243. //            else
  244. //                CustomWindowEvent(kInContentProc, window, &event->where);
  245.     
  246.             break;
  247.  
  248.         case inSysWindow:
  249.             SystemClick(event,window);
  250.             break;
  251.                                                 
  252.         case inDrag:
  253.             if (window != FrontWindow())
  254.                 SelectWindow(window);
  255.             DragWindow(window, event->where,&qd.screenBits.bounds);
  256.             break;
  257.                         
  258.         case inGoAway:
  259.             if (TrackGoAway(window, event->where))
  260.                 RemoveWindow(window);
  261.             break;
  262.                         
  263.         case inZoomIn:
  264.         case inZoomOut:
  265.             if (TrackBox(window,event->where,thePart)) 
  266.                 CustomWindowEvent(kInZoomProc, window,&thePart);
  267.             break;
  268.                         
  269.         case inGrow:
  270. //            CustomWindowEvent(kInGrowProc, window, &event->where);
  271.             break;
  272.     }
  273.     
  274. }
  275.  
  276.  
  277. //----------------------------------------------------------------------
  278. //
  279. //    HandleMenuChoice - 
  280. //                
  281. //
  282. //----------------------------------------------------------------------
  283.  
  284. void HandleMenuChoice(WindowRef window, void *refCon)
  285. {
  286.     long         menuChoice;
  287.     short        item, menu;
  288.     short        daRefNum;
  289.     Str255        daName;
  290.     
  291.     
  292.     menuChoice = *(long *)refCon;
  293.     
  294.     item = LoWord(menuChoice);
  295.     menu = HiWord(menuChoice);
  296.  
  297.     switch(menu) 
  298.     {
  299.         case mApple:
  300.             switch(item) 
  301.             {                    
  302.                 default:
  303.                     GetMenuItemText(GetMenuHandle(mApple),item,daName);
  304.                     daRefNum = OpenDeskAcc(daName);
  305.                     break;
  306.             }    
  307.             break;
  308.                     
  309.         case mFile:
  310.             switch(item) 
  311.             {
  312.                 case iHost:
  313.                     DoHost();
  314.                     break;
  315.                 case iJoin:
  316.                     DoJoin();
  317.                     break;
  318.                 case iLeave:
  319.                     ShutdownNetworking();
  320.                     break;
  321.                 case iQuit:
  322.                     gDone = true;
  323.                     break;
  324.                     
  325.                 default:
  326.                     break;    
  327.             }
  328.             break;
  329.                     
  330.         default:
  331.             HandleNetMenuChoice(menu, item);
  332.             break;    
  333.         
  334.     }
  335.     
  336.     HiliteMenu(0);
  337.     
  338. }
  339.  
  340.  
  341. //----------------------------------------------------------------------
  342. //
  343. //    AdjustMainMenus - 
  344. //                
  345. //
  346. //----------------------------------------------------------------------
  347.  
  348. void AdjustMainMenus(void)
  349. {
  350.     MenuRef        menu;
  351.     
  352.     menu = GetMenuHandle(mFile);
  353.     EnableItem(menu, iHost);
  354.     EnableItem(menu, iJoin);
  355.     EnableItem(menu, iLeave);
  356.  
  357.     if (gNetGame)
  358.     {
  359.         DisableItem(menu, iHost);
  360.         DisableItem(menu, iJoin);
  361.         EnableItem(menu, iLeave);
  362.     }
  363.     else
  364.     {
  365.         EnableItem(menu, iHost);
  366.         EnableItem(menu, iJoin);
  367.         DisableItem(menu, iLeave);
  368.     }    
  369.  
  370.  
  371.     AdjustNetMenus();
  372. }
  373.  
  374.  
  375. //----------------------------------------------------------------------
  376. //
  377. //    HandleContentClick - 
  378. //                
  379. //
  380. //----------------------------------------------------------------------
  381.  
  382. void HandleContentClick(WindowRef window, void *refCon)
  383. {
  384.  
  385. }
  386.  
  387.  
  388. //----------------------------------------------------------------------
  389. //
  390. //    HandleZoomClick - 
  391. //                
  392. //
  393. //----------------------------------------------------------------------
  394.  
  395. void HandleZoomClick(WindowRef window, void *refCon)
  396. {
  397.  
  398. }
  399.  
  400.  
  401. //----------------------------------------------------------------------
  402. //
  403. //    HandleGrow - 
  404. //                
  405. //
  406. //----------------------------------------------------------------------
  407.  
  408. void HandleGrow(WindowRef window, void *refCon)
  409. {
  410.  
  411. }
  412.  
  413.  
  414. //----------------------------------------------------------------------
  415. //
  416. //    UpdateWindow - update dispatcher for document windows.
  417. //                 
  418. //
  419. //----------------------------------------------------------------------
  420.  
  421. void UpdateWindow(WindowRef window) 
  422. {
  423.     GrafPtr        oldPort;
  424.     
  425.     
  426.     GetPort(&oldPort);
  427.     
  428.     SetPort(window);
  429.     BeginUpdate(window);
  430. //    CustomWindowEvent(kUpdateProc, window, nil);
  431.     RefreshWindow(window);
  432.     EndUpdate(window);
  433.     
  434.     SetPort(oldPort);
  435.  
  436. }
  437.  
  438.  
  439. //----------------------------------------------------------------------
  440. //
  441. //    DoActivate - 
  442. //                 
  443. //
  444. //----------------------------------------------------------------------
  445.  
  446. void DoActivate(WindowRef window, void *refCon)
  447. {
  448.     Boolean        becomingActive;
  449.     DocHnd        doc;
  450.     
  451.     SetPort(window);
  452.         
  453.     doc = (DocHnd)GetWRefCon(window);
  454.  
  455.     if(doc != nil && GetIsAppWindow(window)) 
  456.     {
  457.         becomingActive = *(Boolean *)refCon;
  458.         if (becomingActive) 
  459.         {
  460.         }
  461.         
  462.         else 
  463.         {
  464.         }
  465.     }
  466.     
  467. }
  468.  
  469.